﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Infragistics.Win.UltraWinGrid;

namespace BYManageInfoSystem
{
    public partial class Frm_FeeTruePayLock : DockContent
    {
        private DGBY.BLL.DG_FeeTruePayLockTab m_bllDG_FeeTruePayLockTab = new DGBY.BLL.DG_FeeTruePayLockTab();
        private int m_nDelIndex = -1;
        private bool m_bInsertFlag = false;

        public Frm_FeeTruePayLock()
        {
            InitializeComponent();
        }

        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            ShowAllRecord("");
        }

        private void ShowAllRecord(string strWhere)
        {
            DataSet ds = m_bllDG_FeeTruePayLockTab.GetList(strWhere);
            ultraGrid1.DataSource = null;

            ultraGrid1.DataSource = ds.Tables[0];
            ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
        }

        private void buttonProduce_Click(object sender, EventArgs e)
        {
            string strYear = this.dateTimePicker1.Value.Date.Year.ToString();
            string strMonth = this.dateTimePicker1.Value.Date.Month.ToString().PadLeft(2, '0');

            string strYearMonth = strYear + strMonth;
            int nYearMonth = Convert.ToInt32(strYearMonth, 10);

            DataSet ds = m_bllDG_FeeTruePayLockTab.GetList("LockYearMonth = '" + strYearMonth + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                MessageBox.Show("已有该年月记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            AddRecord("商检", nYearMonth);
            AddRecord("报关", nYearMonth);
            AddRecord("大陆船务", nYearMonth);
            AddRecord("香港船务", nYearMonth);
            AddRecord("DHL", nYearMonth);
            AddRecord("单证", nYearMonth);
            AddRecord("纸箱证", nYearMonth);
            AddRecord("检测费", nYearMonth);

            Frm_Main.Log2DB(this.Text, "生成", nYearMonth.ToString());
            ShowAllRecord("");
        }

        private void AddRecord(string strFeeName, int nYearMonth)
        {
            DGBY.Model.DG_FeeTruePayLockTab modelDG_FeeLockTab = new DGBY.Model.DG_FeeTruePayLockTab();
            modelDG_FeeLockTab.FeeName = strFeeName;
            modelDG_FeeLockTab.IsLock = false;
            modelDG_FeeLockTab.LockYearMonth = nYearMonth;
            m_bllDG_FeeTruePayLockTab.Add(modelDG_FeeLockTab);
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }

        private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
        }

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;
            m_nDelIndex = ultraGrid1.ActiveRow.Index;
        }

        private void ultraGrid1_AfterRowCancelUpdate(object sender, RowEventArgs e)
        {
            m_bInsertFlag = false;
        }

        private void ultraGrid1_AfterRowInsert(object sender, RowEventArgs e)
        {
            ultraGrid1.Rows[e.Row.Index].Cells[1].IgnoreRowColActivation = true;
            ultraGrid1.Rows[e.Row.Index].Cells[1].Activation = Activation.AllowEdit;
            ultraGrid1.Rows[e.Row.Index].Cells[1].Activate();
            ultraGrid1.PerformAction(UltraGridAction.EnterEditMode);
            m_bInsertFlag = true;
            InitNewAddRowValue(e.Row.Index);
        }

        private void InitNewAddRowValue(int nIndex)
        {
            UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];

            CurrentUGR.Cells["运输方式"].Value = 1;
            CurrentUGR.Cells["出口方式"].Value = "";
            CurrentUGR.Cells["提单"].Value = 0;
            CurrentUGR.Cells["委托书"].Value = 0;
            CurrentUGR.Cells["放行条"].Value = 0;
            CurrentUGR.Cells["入仓单"].Value = 0;
            CurrentUGR.Cells["六联单"].Value = 0;
            CurrentUGR.Cells["托运单"].Value = 0;

            if (m_bInsertFlag)
            {
                ultraGrid1.Rows[nIndex].Cells[2].Activation = Activation.ActivateOnly;
                m_bInsertFlag = false;
                AddGoodsDetail(nIndex);

            }

        }

        private bool AddGoodsDetail(int nRowIndex)
        {
            return false;
        }

        private void ultraGrid1_BeforeCellCancelUpdate(object sender, CancelableCellEventArgs e)
        {
            m_bInsertFlag = false;
        }

        private void ultraGrid1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {

                case Keys.Up:
                    {
                        UltraGridCell gridCell = this.ultraGrid1.ActiveCell;
                        if (null == gridCell)
                            return;

                        string strTemp = this.ultraGrid1.ActiveCell.Column.Header.Caption;
                        if (strTemp == "运输方式")
                            return;
                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex > 0)
                            nIndex--;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.AboveCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Down:
                    {
                        UltraGridCell gridCell = this.ultraGrid1.ActiveCell;
                        if (null == gridCell)
                            return;

                        string strTemp = this.ultraGrid1.ActiveCell.Column.Header.Caption;
                        if (strTemp == "运输方式")
                            return;
                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex < ultraGrid1.Rows.Count - 1)
                            nIndex++;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.BelowCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Right:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.NextCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
                case Keys.Left:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.PrevCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
            }
        }

        private void ultraGrid1_BeforeRowsDeleted(object sender, BeforeRowsDeletedEventArgs e)
        {

        }

        private bool ModifyGoodsDetail(int nRowIndex)
        {

            try
            {

                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];
                int nSelectFinRecvMoneyDetailTab_NewID = Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString(), 10);
                DGBY.Model.DG_FeeTruePayLockTab modelDG_FeeLockTab = m_bllDG_FeeTruePayLockTab.GetModel(nSelectFinRecvMoneyDetailTab_NewID);


                modelDG_FeeLockTab.FeeName = CurrentUGR.Cells["费用名称"].Value.ToString();
                modelDG_FeeLockTab.LockYearMonth = Convert.ToInt32(CurrentUGR.Cells["应收锁定年月"].Value.ToString(), 10);
                modelDG_FeeLockTab.IsLock = Convert.ToBoolean(CurrentUGR.Cells["是否锁定"].Value.ToString());



                m_bllDG_FeeTruePayLockTab.Update(modelDG_FeeLockTab);


                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        private void ultraGrid1_BeforeRowUpdate(object sender, CancelableRowEventArgs e)
        {
            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[e.Row.Index];

                if (m_bInsertFlag)
                {
                    ultraGrid1.Rows[e.Row.Index].Cells[2].Activation = Activation.ActivateOnly;
                    m_bInsertFlag = false;
                    if (!AddGoodsDetail(e.Row.Index))
                        e.Cancel = true;

                }
                else//修改
                {

                    ModifyGoodsDetail(e.Row.Index);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = true;
            }
        }

    }
}
